import { Component, EventEmitter, Injector, Input, OnInit, Output } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { forEach as _forEach } from 'lodash-es';
import { AppNavigationService } from '@app/shared/layout/nav/app-navigation.service';
import { NameValueDtoOfInt64 } from '@shared/service-proxies/service-proxies';

@Component({
    selector: 'content-menu-multi-selection',
    template:
        `<p-multiSelect  (onClick)="multiSelectChanged($event)" styleClass="form-control"  
         [options]="selectionMenus" [(ngModel)]="selectedValue" 
        [defaultLabel]="defaultLabel" [disabled]="disableMenuData" optionLabel="name" [showToggleAll]="false"></p-multiSelect>`
})
export class ContentMenuMultiSelectionComponent extends AppComponentBase implements OnInit {

    @Input() menuIds: string
    @Output() menuIdsChange = new EventEmitter<string>();

    @Input() disableMenuData: boolean
    @Input() backModuleIds: string

    @Input() defaultVals: string

    backIds: string[] = [];

    selectedValue:NameValueDtoOfInt64[] = [];

    selectionMenus: NameValueDtoOfInt64[] = [];

    defaultLabel ='';

    constructor(
        injector: Injector) {
        super(injector);
    }

    ngOnInit(): void {
        let menuOb = abp.nav2.menus[AppNavigationService.ContentMenuName];
        let menus = JSON.parse(JSON.stringify(menuOb.items));

        if (this.backModuleIds) {
            this.backIds = this.backModuleIds.split(',');
        }

        if (!this.menuIds && this.defaultVals) {
            this.menuIds = this.defaultVals;
            this.menuIdsChange.emit(this.menuIds);
        }

        let menuList :NameValueDtoOfInt64[] = [];
        let defaultValue :NameValueDtoOfInt64[] = [];
        this.getDesMenuList('', menus, menuList, defaultValue);
        this.selectionMenus = menuList;
        this.selectedValue = defaultValue;
    }

    multiSelectChanged(e:any){
        //{originalEvent: event.originalEvent, value: this.value, itemValue: optionValue}
        this.menuIds = this.selectedValue.map(t=> t.value).join(',');
        this.menuIdsChange.emit(this.menuIds);
    }

    private getDesMenuList(prefix: string, menus: abp.nav2.IMtBackEndUserMenuItem[], 
        menuList:NameValueDtoOfInt64[],defaultValue:NameValueDtoOfInt64[]) {
        //selectionMenus, selectedValue

        for (const item of menus) {
            let backEndModuleId = item.backEndModuleId;
            
            if (this.backIds.indexOf(backEndModuleId) > -1) {
                menuList.push(new NameValueDtoOfInt64({
                    name: (prefix? prefix + '/' + item.displayName: item.displayName),
                    value: item.auxiliaryId
                }))
            }

            if((','+this.menuIds+',').indexOf(item.auxiliaryId.toString()) >= 0){
                defaultValue.push(new NameValueDtoOfInt64({
                    name: (prefix? prefix + '/' + item.displayName: item.displayName),
                    value: item.auxiliaryId
                }))
            }

            if (item.items.length > 0) {
                this.getDesMenuList((prefix? prefix + '/' + item.displayName: item.displayName), item.items, menuList, defaultValue);
            }
        }
    }



    /*
    private recursionMenu(menus:abp.nav2.IMtBackEndUserMenuItem[],desCodes:string[]){
        for (const item of menus) {
            let backEndModuleId  = item.backEndModuleId;
            if (this.backIds.indexOf(backEndModuleId) > -1){
                desCodes.push(item.code);
            }
            if (item.items.length > 0){
                this.recursionMenu(item.items,desCodes);
            }
        }
    }

    private getFilterMenus(menus:abp.nav2.IMtBackEndUserMenuItem[],desCodes:string[]){
        for (let index = menus.length - 1; index >= 0; index--) {
            const elm = menus[index];
            if (!this.checkCode(elm.code,desCodes)){
                menus.splice(index, 1);
            }
            else{
                this.getFilterMenus(elm.items,desCodes);
            }
        }
    }

    private checkCode(code:string,desCodes:string[]):boolean{
        let cLen = code.length;
        for (const desCode of desCodes) {
            if (code == desCode) return true;
            let s = desCode.substr(0,cLen);
            if (code == s) return true;
        }

        return false;
    }

    private getMenuList(items:abp.nav2.IMtBackEndUserMenuItem[],menuList:abp.nav2.IMtBackEndUserMenuItem[]){
        for (const item of items) {
            let arrCode = item.code.split('.');
            item.displayName = Array(arrCode.length).join('---') + ' ' + item.displayName;
            menuList.push(item);
            if (item.items.length > 0){
                this.getMenuList(item.items,menuList)
            }
        }
      
    }*/


}
